package cn.tedu.crm.dept.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.dept.pojo.dto.DeptParam;
import cn.tedu.crm.dept.pojo.dto.DeptQuery;
import cn.tedu.crm.dept.pojo.vo.DeptVO;
import cn.tedu.crm.dept.service.DeptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("api/crm/personnel/dept")
@Api(tags = "部门管理")
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("select")
    public JsonResult selectDept(DeptQuery query) {
        log.debug("查询部门信息:{}", query);
        PageObject<DeptVO> pageObject = PageObject.page(
                query.getPageNum(), query.getPageSize(), () -> deptService.selectDept(query));
        return JsonResult.success(pageObject);
    }

    @PostMapping("add")
    @SaCheckPermission("crm:personnel:dept:add")
    public JsonResult addDept(@Validated @RequestBody DeptParam deptParam){
        log.debug("添加部门:{}",deptParam);
        deptService.addDept(deptParam);
        return JsonResult.success(deptParam);
    }


    /**
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    @SaCheckPermission("crm:personnel:dept:del")
    public JsonResult deleteDept( Long ids) {
        log.debug("删除角色：{}",ids);
        deptService.deleteDept(ids);
        return JsonResult.ok();
    }

    @GetMapping("selectById")
    public JsonResult selectById( Long id) {
        log.debug("根据id查询部门信息:{}", id);
        DeptVO deptVO = deptService.selectById(id);
        return JsonResult.success(deptVO);
    }



}
